//
//  OrderListViewController.m
//  xingfugangwan
//
//  Created by rainday on 15/1/1.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "OrderListViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "OrdetailViewController.h"


// viewmodel

#import "OrderListViewModel.h"
@interface OrderListViewController ()<UITableViewDelegate,UITableViewDataSource,OrderDetailProtocol>{
    NSString *_status;
    

}

@property (nonatomic ,strong) OrderListViewModel *orderListViewModel;
@property (nonatomic ,weak) IBOutlet UITableView *tableView;


@end

static NSString *reuserIdentify = @"Cell";


@implementation OrderListViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil withStatus:(NSString *)status
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        _status = status;
        
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _orderListViewModel = [[OrderListViewModel alloc] init];
    RACSignal *downedDataSignal = [_orderListViewModel fetchOrderDataWithStatus:_status];
    @weakify(self)
    
    [[[downedDataSignal filter:^BOOL(id value) {
        @strongify(self)
       
        BOOL contion = [value isEqualToString:@"success"]?YES:NO;
        if (contion) {
            [self.tableView reloadData];
            
        }
        return contion;
    }]
    deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        
    }];
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_orderListViewModel numberOfRowsInSection:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentify];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [_orderListViewModel textForRowAtIndexPath:indexPath];
    
   
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrdetailViewController *detailVC = [[OrdetailViewController alloc] initWithNibName:@"OrdetailViewController" bundle:nil withOrderId:[_orderListViewModel getOrderIdForIndexPath:indexPath]];
    detailVC.delegate = self;
    detailVC.navigationItem.title = [_orderListViewModel getOrderIdForIndexPath:indexPath];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dismissOrdetailViewController:(OrdetailViewController *)orderViewController
{
    [orderViewController dismissViewControllerAnimated:YES completion:nil];
    
}
@end
